package com.module.shici.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.fenghuajueli.libbasecoreui.utils.MyStatusBarUtils;
import com.module.shici.R;
import com.module.shici.databinding.ModuleShiciActivityShiCiDetailBinding;
import com.xunfei.yuyin.tool.XunfeiVoiceTool;

import java.util.HashMap;

public class ShiCiDetailActivity extends AppCompatActivity implements View.OnClickListener {

    private ModuleShiciActivityShiCiDetailBinding binding;
    private String content, fanyi, zhushi;
    private ImageView currentTab;//当前选中的tab
    private Boolean voiceFlag = false;//是否朗读
    private HashMap<String,String> params = new HashMap<String, String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyStatusBarUtils.setStatusBar(this,"#F6EEE4");
        binding = ModuleShiciActivityShiCiDetailBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        XunfeiVoiceTool.getInstance().init(this,"a598391c","xiaoyan");
        //初始化数据
        initData();
    }

    private void initData() {
        //返回
        binding.shiciDetailBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        //诗词类型
        getIntentData(binding.shiciDetailType, "type");
        //诗词标题
        getIntentData(binding.shiciDetailTitle, "title");
        //朝代 作者
        binding.shiciDetailAuthor.setText("[" + getIntent().getStringExtra("chaodai") + "]" + getIntent().getStringExtra("author"));
        //内容
        content = getIntent().getStringExtra("content").replaceAll("。", "。\n");
        binding.shiciDetailContent.setText(content);
        //默认选中正文
        binding.shiciDetailZw.setSelected(true);
        //将当前选中的值赋给 currentTab
        currentTab = binding.shiciDetailZw;
        //初始化点击事件
        binding.shiciDetailZw.setOnClickListener(this);
        binding.shiciDetailZs.setOnClickListener(this);
        binding.shiciDetailFy.setOnClickListener(this);
        binding.shiciDetailYy.setOnClickListener(this);
    }

    private void getIntentData(TextView tv, String name) {
        tv.setText(getIntent().getStringExtra(name));
    }

    @Override
    public void onClick(View v) {
        if (v.getId() != R.id.shici_detail_yy){
            currentTab.setSelected(false);
        }
        if (v.getId() == R.id.shici_detail_zw) {
            //正文
            binding.shiciDetailContent.setText(content);
            binding.shiciDetailZw.setSelected(true);
            currentTab = binding.shiciDetailZw;
        } else if (v.getId() == R.id.shici_detail_zs) {
            //注释
            if (null == zhushi) {
                zhushi = getIntent().getStringExtra("zhushi").replaceAll("。", "。\n");
            }
            binding.shiciDetailContent.setText(zhushi);
            //现代诗没有注释与翻译
            if (getIntent().getStringExtra("type").equals("现代诗")){
                binding.shiciDetailContent.setText("现代诗没有注释与翻译哦");
            }
            binding.shiciDetailZs.setSelected(true);
            currentTab = binding.shiciDetailZs;
        } else if (v.getId() == R.id.shici_detail_fy) {
            //翻译
            if (fanyi == null) {
                fanyi = getIntent().getStringExtra("fanyi").replaceAll("。", "。\n");
            }
            binding.shiciDetailContent.setText(fanyi);
            //现代诗没有注释与翻译
            if (getIntent().getStringExtra("type").equals("现代诗")){
                binding.shiciDetailContent.setText("现代诗没有注释与翻译哦");
            }
            binding.shiciDetailFy.setSelected(true);
            currentTab = binding.shiciDetailFy;
        }
        if (v.getId() == R.id.shici_detail_yy) {
            //语音
            voiceFlag = !voiceFlag;
            binding.shiciDetailYy.setSelected(voiceFlag);
            // 需要参数才能使用回调,params 是随便写的
            if (binding.shiciDetailYy.isSelected()){
                XunfeiVoiceTool.getInstance().speek(content);
            }else {
                XunfeiVoiceTool.getInstance().stop();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        XunfeiVoiceTool.getInstance().destroy();
    }
}